package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.ShopDetailEntity;
import com.eightbears.beboy.entity.UserEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.eightbears.beboy.ui.merchant.activity.ShopApplyActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOPS_DETAILS;
import static com.eightbears.beboy.ui.merchant.activity.ShopApplyActivity.EVENT_UPDATE_SHOP;

public class ShopInfoActivity extends BaseActivity {

    public static final String PARAMS_SHOP_DETAIL = "shopDetail";

    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.tv_avg_price)
    TextView tv_avg_price;
    @BindView(R.id.iv_shop)
    ImageView iv_shop;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_shop_title)
    TextView tv_shop_title;
    @BindView(R.id.tv_dis)
    TextView tv_dis;
    @BindView(R.id.ll_right)
    LinearLayout ll_right;

    private String shopId;
    private ShopDetailEntity shopDetailEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_info);
        EventBus.getDefault().register(this);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        UserEntity userEntity = SPUtil.getUser();
        shopId = userEntity.getData().getShops().get(0).getShop_id();
        ll_right.setVisibility(View.VISIBLE);
        tv_title.setText(R.string.text_shop_info);
    }

    @Override
    protected void initData() {
        getShopInfo();
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_right)
    void ll_right() {
        startLocalActivity(this, ShopApplyActivity.class, PARAMS_SHOP_DETAIL, shopDetailEntity);
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    private void getShopInfo() {
        String url = URL_SHOPS_DETAILS + shopId;
        OkHttpUtils.get().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                shopDetailEntity = GsonUtil.fromJson(response, ShopDetailEntity.class);
                if (shopDetailEntity.getStatus_code().equals(CONNECTION_STATUS_SUCCESS)) {
                    initShopInfo();
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(ShopInfoActivity.this, status_error);
            }
        });
    }

    private void initShopInfo() {
        CommonUtils.loadImageView(this, shopDetailEntity.getData().getCover(),
                R.mipmap.icon_default_shop, iv_shop);
        String price = String.format(mContext.getText(R.string.text_price).toString(),
                shopDetailEntity.getData().getConsumption());
        tv_avg_price.setText(price);
        tv_shop_title.setText(shopDetailEntity.getData().getTitle());
        tv_address.setText(shopDetailEntity.getData().getAddress());
        tv_address.setText(shopDetailEntity.getData().getAddress());
        tv_phone.setText(shopDetailEntity.getData().getPhone());
        tv_dis.setText(shopDetailEntity.getData().getContent().getContent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataSynEvent(String event) {
        switch (event) {
            case EVENT_UPDATE_SHOP:
                initData();
                break;
        }
    }
}
